package com.demo.shardingsphere.supplier.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;

/**
 * 分库分表配置
 */
@Configuration
public class SupplierShardingConfig {

    @Bean
    public DataSource shardingCustomer() throws SQLException {

        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();

        //  配置多数据源
        Map<String, DataSource> dataSourceMap = getDatasourceMap();
        //  其它配置信息
        Properties properties = new Properties();
        // 打印出分库路由后的sql
        properties.setProperty("sql.show", "true");

        //  (分库分表)策略配置
        List<TableRuleConfiguration> allTableRuleConfiguration = getAllTableRuleConfiguration();

        shardingRuleConfig.getTableRuleConfigs().addAll(allTableRuleConfiguration);
        //   创建并返回sharding总数据源,注入容器
        return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, properties);
    }


    public static List<TableRuleConfiguration> getAllTableRuleConfiguration() {
        List<TableRuleConfiguration> list = new ArrayList<>(8);
        //用户id分表订单表配置
        TableRuleConfiguration supplierIdRule = getSupplierIdTableRuleConfiguration();

        list.add(supplierIdRule);
        return list;
    }

    /**
     *  商户id 分库分表规则配置
     * @return
     */
    private static TableRuleConfiguration getSupplierIdTableRuleConfiguration() {
        //  逻辑表名 t_order$
        String nodes = "db0.t_order_supplier${0..3}";
        //表规则配置
        TableRuleConfiguration staffTableRuleConfig = new TableRuleConfiguration("t_supplier_order", nodes);
        //数据库分片
        staffTableRuleConfig.setDatabaseShardingStrategyConfig(
                new StandardShardingStrategyConfiguration("supplier_id", new SupplierDatabaseAlgorithm()));
        //表分片
        StandardShardingStrategyConfiguration standardShardingStrategyConfiguration = new StandardShardingStrategyConfiguration(
                "supplier_id", new SupplierTableAlgorithm());
        staffTableRuleConfig.setTableShardingStrategyConfig(standardShardingStrategyConfiguration);

        return staffTableRuleConfig;
    }

    public static Map<String, DataSource> getDatasourceMap() {

        Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(4);

        DruidDataSource db0 = new DruidDataSource();
        db0.setDriverClassName("com.mysql.cj.jdbc.Driver");
        db0.setUrl("jdbc:mysql://106.12.59.2:3309/db0?characterEncoding=utf8&serverTimezone=GMT%2B8");
        db0.setUsername("root");
        db0.setPassword("1225@root@1225");
        dataSourceMap.put("db0", db0);

        return dataSourceMap;
    }

}